using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using IReaper.FileData;
using IReaper.Running;
using WeifenLuo.WinFormsUI.Docking;

namespace IReaper.CourseData
{
    public partial class CoursesPanel : iReaperDockContent
    {
        public CoursesPanel():base()
        {
            InitializeComponent();

            Core.CoreData.PropertyChanged += new PropertyChangedEventHandler(CoreData_PropertyChanged);
        }

        void CoreData_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            if (e.PropertyName == CoreDataType.CurrentViewedCourses.ToString())
            {
                this.txtBoxQuery.Clear();
            }
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            CourseCollection currentView = (CourseCollection)Core.CoreData[CoreDataType.CurrentViewedCourses];
            string query = this.txtBoxQuery.Text;
            try
            {
                var result = CourseDataManager.Search(query, currentView);
                Core.CoreData[CoreDataType.CurrentFilteredCourses] = result;
            }
            catch(Lucene.Net.QueryParsers.ParseException ex)
            {
                MessageBox.Show(ex.Message, "Query string error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void OnQueryStringEdit(object sender, EventArgs e)
        {
            this.btnSearch.Enabled = this.txtBoxQuery.Text.Length > 0;

            if (this.txtBoxQuery.Text.Length == 0)
            {
                Core.CoreData[CoreDataType.CurrentViewedCourses] = Core.CoreData[CoreDataType.CurrentViewedCourses];
            }
        }

        private void btnReset_Click(object sender, EventArgs e)
        {
            this.txtBoxQuery.Clear();
        }
    }
}